package com.jz.digital.net;


import com.jz.digital.utils.LogUtils;
import com.jz.digital.utils.SPHelper;

import java.io.IOException;
import java.net.Proxy;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitService {
    private static final RetrofitService mService = new RetrofitService();
    private Retrofit mRetrofit;

    public static RetrofitService getInstance() {
        return mService;
    }


    private RetrofitService() {
    }

    public void init() {
        mRetrofit = initRetrofit();
    }

    private Retrofit initRetrofit() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .connectTimeout(NetConfiguration.Builder.TimeOut, TimeUnit.MILLISECONDS)
                .readTimeout(NetConfiguration.Builder.TimeOut, TimeUnit.MILLISECONDS)
                .writeTimeout(NetConfiguration.Builder.TimeOut, TimeUnit.MILLISECONDS)
                .sslSocketFactory(SSLSocketClient.getSSLSocketFactory(), SSLSocketClient.getX509TrustManager())
                .proxy(Proxy.NO_PROXY);
        OkHttpClient mOkHttpClient = builder.build();
        LogUtils.d(">>>>>>>>>>>>",SPHelper.getLocalLanAddress());
        return new Retrofit.Builder()
                .client(mOkHttpClient)
                .baseUrl(SPHelper.getLocalLanAddress())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public static <T> T create(final Class<T> service) {
        return mService.mRetrofit.create(service);
    }

    private Interceptor getDownloadInterceptor() {
        return new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                Request build = request.newBuilder()
                        .addHeader("Accept-Encoding", "identity")
                        .addHeader("contentType", "text/json")
                        .build();
                return chain.proceed(build);
            }
        };
    }
}
